package com.gnerv.tools.entity.extend.utils.mapper;

import com.gnerv.tools.entity.extend.annotation.EntityExtend;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.ResultMap;
import org.apache.ibatis.session.Configuration;

import java.util.List;
import java.util.Set;

public class MappedStatementUtils {

    private MappedStatementUtils() {
    }

    public static MappedStatement buildMappedStatement(MappedStatement ms, Set<EntityExtend> entityExtends) {
        Configuration configuration = ConfigurationUtils.buildConfiguration(ms, entityExtends);
        MappedStatement.Builder builder = new MappedStatement.Builder(configuration, ms.getId(), ms.getSqlSource(), ms.getSqlCommandType());
        builder.resource(ms.getResource());
        builder.fetchSize(ms.getFetchSize());
        builder.statementType(ms.getStatementType());
        builder.keyGenerator(ms.getKeyGenerator());
        builder.timeout(ms.getTimeout());
        builder.parameterMap(ms.getParameterMap());

        List<ResultMap> resultMaps = ResultMapUtils.buildResultMap(ms, entityExtends);

        builder.resultMaps(resultMaps);
        builder.resultSetType(ms.getResultSetType());
        builder.cache(ms.getCache());
        builder.flushCacheRequired(ms.isFlushCacheRequired());
        builder.useCache(ms.isUseCache());
        return builder.build();
    }

}
